﻿using AutoMapper;
using Core.Infrastructures.Cores.Models;
using Core.Repositories.Menu;
using Core.Repositories.Menu.Entities;
using Core.Services.Menu.Dtos;
using System;
using System.Collections.Generic;
using System.Text;

namespace Core.Services.Menu
{
    public partial class MenuService: BaseService<MenuEntity, MenuDto>, IMenuService
    {
        public MenuService(IMapper mapper,
            IMenuRepository menuRepository)
        {
            _mapper = mapper;
            _repository = menuRepository;
        }

        public override (int, HandleResult) Insert(MenuDto dto)
        {
            if (_repository.Any(u => u.UniqueId.ToLower() == dto.UniqueId.ToLower())) return (0, Result("exist"));

            var entity = _mapper.Map<MenuEntity>(dto);
            entity.CreateTime = DateTime.Now;
            entity.CreateBy = "System";

            return (_repository.Insert(entity), Result());
        }

        public override (int, HandleResult) Delete<TKey>(TKey id)
        {
            if (_repository.Any(it => it.ParentId == Convert.ToInt32(id))) return (0, new HandleResult("quote"));

            return base.Delete(id);
        }

    }
}
